<?php
declare (strict_types = 1);
namespace app\api\controller;

use think\Request;
use think\Exception;
use think\exception\ValidateException;

use app\api\controller\BaseController;
use app\common\model\ApplyInduction;
use service\JsonService as Json;
use service\PayService;

use app\common\model\User;
use app\common\model\Product;
use app\common\model\ProductOrder;
use app\common\model\UserSection;
use app\common\model\UserSectionLevel;

class ProductController extends BaseController
{
    /**
     * 获取产品列表
     */
    public function getProductList()
    {
        $sectionId = Request()->param('section_id');

        $where = [];

        $sectionInfo = UserSection::getFind(['id' => $sectionId]);

        if($sectionInfo['is_out'] == 1) $where[] = ['id', '=', 5];

        $list = Product::getList($where, 0);
        return Json::success('ok', $list);
    }

    /**
     * 下单支付
     */
    public function payProduct()
    {
        $productId = Request()->param('product_id', 0);
        $num       = Request()->param('num', 1);

        /** 产品信息 */
        $productInfo = Product::getFind(['id' => $productId]);
        if(!$productInfo) return Json::fail('产品不存在');

        /** 用户信息 */
        $userInfo = User::getFind(['id' => $this->user_id], 'openid');

        $sectionId = ApplyInduction::getVal(['user_id' => $this->user_id], 'section_id');

        /** 订单号 */
        $orderNo = getOrderNo();

        if($productInfo['is_installment']){
            $price = $productInfo['down_payment'];
        }else{
            $price = $productInfo['total'];
        }

        $data = [
            'order_no'             => $orderNo,
            'user_id'              => $this->user_id,
            'section_id'           => $sectionId,
            'product_id'           => $productId,
            'price'                => $price,
            'num'                  => $num,
            'installment_price'    => $productInfo['installment_price'],
            'installment_nums'     => $productInfo['is_installment'] ? 1 : 0,
            'installment_nums_all' => $productInfo['installment_nums'],
            'is_installment'       => $productInfo['is_installment'],
            'product_info'         => json_encode($productInfo),
            'pay_type'             => 2,
            'invoice'              => $productInfo['invoice'],
        ];
        
        $flag = ProductOrder::add($data);
        if(!$flag) return Json::fail('下单失败，请稍后重试!');

        $order = [
            'out_trade_no' => $orderNo,
            'description' => '购买套餐',
            'amount' => [
                //  'total' => $price * 100,
                 'total' => 1,
            ],
            'payer' => [
                 'openid' => $userInfo['openid'],
            ],
        ];

        $result = PayService::createMiniProductOrder($order);

        if($result['appId'] && $result['package'] && $result['paySign']){
            return Json::success('ok', $result->toArray());
        }

        return Json::fail('下单失败，请稍后重试!');
    }

    /**
     * 支付回调
     */
    public function product_notify()
    {
        PayService::notifyCallback();
    }
}